<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="盘点仓库">
              <j-dict-select-tag
                placeholder="请选择仓库"
                v-model="queryParam.warehouseCode"
                dictCode="wms_warehouse,warehouse_name,warehouse_code" />
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="盘点类型">
              <j-dict-select-tag placeholder="请选择盘点类型" v-model="queryParam.inventoryType"
                                 dictCode="inventory_type" />
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="盘点年月">
                <j-date placeholder="请选择盘点年月" v-model="queryParam.inventoryMonth"
                        date-format="YYYY-MM"></j-date>
              </a-form-item>
            </a-col>
          </template>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'" />
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button
        v-if="status === 0"
        @click="handleAdd"
        type="primary"
        icon="plus">新增
      </a-button>
      <a-button
        v-if="status !== 0"
        type="primary"
        icon="download"
        @click="handleExportXls('库存盘点表')">导出
      </a-button>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择
        <a style="font-weight: 600">{{ selectedRowKeys.length }}</a
        >项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
        <span style="float:right;">
          <a-popover title="自定义列" trigger="click" placement="leftBottom">
            <template slot="content">
              <a-checkbox-group @change="onColSettingsChange" v-model="settingColumns" :defaultValue="settingColumns">
                <a-row style="width: 400px">
                  <template v-for="(item,index) in defColumns">
                    <template v-if="item.key!=='rowIndex'&& item.dataIndex!=='action'">
                        <a-col :span="12"><a-checkbox :value="item.dataIndex"><j-ellipsis :value="item.title" :length="10"></j-ellipsis></a-checkbox></a-col>
                    </template>
                  </template>
                </a-row>
              </a-checkbox-group>
            </template>
            <a><a-icon type="setting" />设置</a >
          </a-popover>
        </span>
      </div>

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        class="j-table-force-nowrap1"
        :scroll="{x:true}"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        @change="handleTableChange">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text,record">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" :preview="record.id" height="25px" alt=""
               style="max-width:80px;font-size: 12px;font-style: italic;" />
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record" class="action">
          <approval-buttons
            :status="status"
            @success="loadData"
            :data-id="record.id"
            :service-name="serviceName"
            :variables="record"
          />
          <a @click="handleEdit(record)" v-if="[0,1].includes(status)">编辑</a>
          <a @click="handleDetail(record)">详情</a>
          <a-dropdown v-if="![2,3].includes(status)">
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item v-if="![2,3].includes(status)">
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>

      </a-table>
    </div>

    <inventory-count-modal ref="modalForm" @ok="fetchData" />
  </a-card>
</template>

<script>

import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import InventoryCountModal from './modules/InventoryCountModal'
import { filterMultiDictText } from '@/components/dict/JDictSelectUtil'
import '@/assets/less/TableExpand.less'
import dayjs from 'dayjs'
import { revokeProcess } from '@views/flowable/api/finished'
import ApprovalButtons from '@comp/approval/ApprovalButtons.vue'

export default {
  name: 'InventoryCountList',
  mixins: [JeecgListMixin],
  components: {
    ApprovalButtons,
    InventoryCountModal
  },
  props: {
    status: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      description: '库存盘点表管理页面',
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function(t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '盘点单号',
          align: 'center',
          dataIndex: 'inventoryCountNo'
        },
        {
          title: '仓库名称',
          align: 'center',
          dataIndex: 'warehouseCode_dictText'
        },
        {
          title: '盘点年月',
          align: 'center',
          dataIndex: 'inventoryMonth',
          customRender: function(text) {
            return !text ? '' : (text.length > 10 ? text.substr(0, 10) : text)
          }
        },
        {
          title: '制单人',
          align: 'center',
          dataIndex: 'createBy_dictText'
        },
        {
          title: '制单部门',
          align: 'center',
          dataIndex: 'sysOrgCode_dictText'
        },
        {
          title: '制单日期',
          align: 'center',
          dataIndex: 'createTime',
          customRender: function(text) {
            return !text ? '' : (text.length > 10 ? text.substr(0, 10) : text)
          }
        },
        {
          title: '盘点类型',
          align: 'center',
          dataIndex: 'inventoryType_dictText'
        },
        {
          title: '备注',
          align: 'center',
          dataIndex: 'remark'
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: '/wms/inventory/inventory-count/list',
        delete: '/wms/inventory/inventory-count/delete',
        deleteBatch: '/wms/inventory/inventory-count/deleteBatch',
        exportXlsUrl: '/wms/inventory/inventory-count/exportXls'
      },
      dictOptions: {},
      superFieldList: [],
      serviceName: 'inventoryCountService',
      settingColumns: [],
      disableMixinCreated: true
    }
  },
  created() {
    this.queryParam.status = this.status
    this.loadData()
  },
  computed: {
    importExcelUrl: function() {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    }
  },
  methods: {
    beforeSearch() {
      if (this.queryParam.inventoryMonth) {
        this.queryParam.inventoryMonth = dayjs(this.queryParam.inventoryMonth).format('YYYY-MM-DD')
      }
      return true
    },
    fetchData() {
      this.beforeSearch()
      this.modalFormOk()
    },
    searchReset() {
      this.queryParam = {
        status: this.status
      }
      this.loadData(1)
    },
    initDictConfig() {
    }
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';

span.action {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 16px;
}

.action a {
  position: relative;
}

.action a:not(:first-child)::before {
  content: "";
  position: absolute;
  left: 0;
  width: 1px;
  height: 0.9em;
  background-color: #e8e8e8;
  margin: 0 -8px;
}
</style>